#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>

typedef int BTDatatype;


typedef struct BinaryTreeNode
{
	BTDatatype data;
	struct BinaryTreeNode* left;
	struct BinaryTreeNode* right;

}BTNode;
BTNode* BuyBTNode(BTDatatype x)
{
	BTNode* newnode = (BTNode*)malloc(sizeof(BTNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	newnode->data = x;
	newnode->left = NULL;
	newnode->right = NULL;

	return newnode;

}

int TreeSize(struct BinaryTreeNode* root)
{
	return root == NULL ? 0 : TreeSize(root->left) + TreeSize(root->right) + 1;
}

void preorder(struct BinaryTreeNode* root,int* a,int* i)
{
	if (root == NULL)
	{
		return;
	}

	a[(*i)++] = root->data;
	preorder(root->left, a, i);
	preorder(root->right, a, i);

}
int* preorderTraversal(struct BinaryTreeNode* root, int* returnSize)
{
	*returnSize = TreeSize(root);
	int* a = (int*)malloc(*returnSize * sizeof(int));
	int i = 0;
	preorder(root, a, &i);


	return a;
}

int main()
{
	BTNode* n1 = BuyBTNode(1);
	BTNode* n2 = BuyBTNode(2);
	BTNode* n3 = BuyBTNode(3);
	BTNode* n4 = BuyBTNode(4);
	BTNode* n5 = BuyBTNode(5);
	BTNode* n6 = BuyBTNode(6);
	BTNode* n7 = BuyBTNode(7);
	n1->left = n2;
	n1->right = n4;
	n2->left = n3;
	n4->left = n5;
	n4->right = n6;
	n3->left = n7;
	int i = 7;
	preorderTraversal(n1, &i);
	return 0;
}